package priv.hxm.view;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JTextArea;
import javax.swing.JTextField;

import org.apache.log4j.Logger;

import priv.hxm.speakandlisten.Listen;
import priv.hxm.speakandlisten.Speak;
import priv.mill.net.MessageContexts;
import priv.mill.net.MessageObj;
import priv.mill.net.MyHttp;

public class GameListener implements ActionListener {
	private static Logger logger = Logger.getLogger(GameListener.class);
	JTextField sendMsg;
	JTextArea showMsg;
	String type;
	Listen listen = Listen.getInstance();
	Speak speak = Speak.getInstance();
	private JTextArea my;
	private JTextArea cat;

	public GameListener(JTextField sendMsg, JTextArea showMsg, String type,
			JTextArea my, JTextArea cat) {// 构造器
		this.sendMsg = sendMsg;
		this.showMsg = showMsg;
		this.type = type;
		this.my = my;
		this.cat = cat;
	}

	public GameListener() {
	}

	/*
	 * 石头：1 剪刀：2 布：3
	 */
	String[] types = { "石头", "剪刀", "布" };

	@Override
	public void actionPerformed(ActionEvent arg0) {
		MessageContexts.getInstance().offerMessage(new MessageObj() {

			@Override
			public void handlerMessage(MyHttp info) {
				Object data = info.getData();
				String string = (String) data;
				showMsg.append("虹小猫：" + string + "\n");
				cat.setText("");
				cat.append("\n" + string);
				showMsg.setCaretPosition(showMsg.getDocument().getLength());// 重新定位焦点保证新加的文字能显示出来
				speak.shuo(string);

			}

			@Override
			public MyHttp getMessage() {
				MyHttp myHttp = new MyHttp();
				showMsg.append("我：" + types[Integer.valueOf(type) - 1] + "\n");
				my.setText("");// 清空上一次
				my.append("\n" + types[Integer.valueOf(type) - 1]);// 显示在聊天界面
				showMsg.setCaretPosition(showMsg.getDocument().getLength());// 重新定位焦点保证新加的文字能显示出来
				myHttp.setData(type);
				myHttp.setMyUrl("Plays");
				return myHttp;
			}
		});
	}
}
